import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Random;

public class Prison {

	public static int ROWS;
	public static int COLUMN;
	public static Element[][] stanza;
	private static Element entry = new Entry();
	private static Element exit = new Exit();
	private static Weapon ascia = new Weapon("Ascia", 20);
	public String[][] tempArray=new String[ROWS][COLUMN];

	public Prison(int rows, int column) {
		ROWS = rows;
		COLUMN = column;
		stanza = new Element[ROWS][COLUMN];
	}

	public void creaStanza() {
		Random r = new Random();
		char c;
		int i, j, a, b;
		int l = 15; 				// variabile per limitare le pozioni
		String range = "abc"; 		// range di lettere, da cui ottenere casualmente i
									// vari elementi
		i = r.nextInt(ROWS);
		j = r.nextInt(COLUMN);
		setEl(i, j, entry);
		do {
			a = r.nextInt(ROWS);
			b = r.nextInt(COLUMN);
		} while ((i == a) && (j == b));
		setEl(a, b, exit);

		for (int x = 0; x < ROWS; x++) {
			for (int y = 0; y < COLUMN; y++) {
				if (getEl(x, y) == entry) {
					setEl(x, y, Admin.player);
				}
				if (getEl(x, y) == null) {
					c = range.charAt(r.nextInt(range.length()));
					if (c == 'a') {
						setEl(x, y, new Wall());
					}
					else if (c == 'b') {
						if (l > 0) {
							setEl(x, y, new Potion());
							l--;
						} else {
							setEl(x, y, new Empty());
						}
					}
					else if (c == 'c') {
						setEl(x, y, new Monster(50, 25, ascia));
					}
				}
			}
		}
	}
	
	public static Element getEl(int x, int y) { // metodo per ritornare l'elemento
		return stanza[x][y];
	}

	public static void setEl(int x, int y, Element el) { 
		stanza[x][y] = el;
	}

	public int getPosx(Element el) {
		int posx = 0;
		for (int x = 0; x < ROWS; x++) {
			for (int y = 0; y < COLUMN; y++)
				if (getEl(x, y) == el) {
					posx = x;
				}
		}
		return posx;
	}

	public int getPosy(Element el) {
		int posy = 0;
		for (int x = 0; x < ROWS; x++) {
			for (int y = 0; y < COLUMN; y++)
				if (getEl(x, y) == el) {
					posy = y;
				}
		}
		return posy;
	}

	public static int getROWS() {
		return ROWS;
	}

	public static void setROWS(int rOWS) {
		ROWS = rOWS;
	}

	public static int getCOLUMN() {
		return COLUMN;
	}

	public static void setCOLUMN(int cOLUMN) {
		COLUMN = cOLUMN;
	}

	public static Element[][] getStanza() {
		return stanza;
	}

	public static void setStanza(Element[][] stanza) {
		Prison.stanza = stanza;
	}

	public static Element getEntry() {
		return entry;
	}

	public static void setEntry(Element entry) {
		Prison.entry = entry;
	}

	public static Element getExit() {
		return exit;
	}

	public static void setExit(Element exit) {
		Prison.exit = exit;
	}

	public boolean victory(int x, int y) {
			if (getEl(x, y) instanceof Exit)
				return true;
			else
				return false;
	}

	public boolean findMonster(int x, int y) {
			if (getEl(x, y) instanceof Monster)
				return true;
			else
				return false;
	}
	
	public boolean findWall(int x, int y){
		if (getEl(x, y) instanceof Wall){
			return true;
		}
		else return false;
	}

	public void visualizzaStanzaCompleta() {
		for (int x = 0; x < ROWS; x++) {
			System.out.print("\n+---++---++---++---++---++---++---+\n");
			for (int y = 0; y < COLUMN; y++){
				if(Prison.getEl(x, y) instanceof Player)
					System.out.print("| G |");
				else if(Prison.getEl(x, y) instanceof Monster)
					System.out.print("| m |");
				else if(Prison.getEl(x, y) instanceof Potion)
					System.out.print("| p |");
				else if(Prison.getEl(x, y) instanceof Wall)
					System.out.print("| w |");
				else if(Prison.getEl(x, y) instanceof Empty)
					System.out.print("| v |");
				else if(Prison.getEl(x, y) instanceof Exit)
					System.out.print("| U |");
			}
		}
		System.out.print("\n+---++---++---++---++---++---++---+\n");
	}

	public void visualizzaStanza() {
		for (int x = 0; x < ROWS; x++) {
			System.out.print("\n+---++---++---++---++---++---++---+\n");
			for (int y = 0; y < COLUMN; y++){
				if(Prison.getEl(x, y) instanceof Player)
					System.out.print("| G |");
				else if(Prison.getEl(x, y) instanceof Exit)
					System.out.print("| U |"); 
				else
					System.out.print("| * |");
			}
		System.out.print("\n+---++---++---++---++---++---++---+\n");
		}
	}

	public void save() {
		try {
			FileOutputStream prova = new FileOutputStream("Salvataggio.txt");
			@SuppressWarnings("resource")
			PrintStream scrivi = new PrintStream(prova);
			for (int i = 0; i < ROWS; i++) {
				for (int j = 0; j < COLUMN; j++){
					if(Prison.getEl(i, j) instanceof Player)
						scrivi.print("G ");
					else if(Prison.getEl(i, j) instanceof Monster)
						scrivi.print("m ");
					else if(Prison.getEl(i, j) instanceof Potion)
						scrivi.print("p ");
					else if(Prison.getEl(i, j) instanceof Wall)
						scrivi.print("w ");
					else if(Prison.getEl(i, j) instanceof Empty)
						scrivi.print("v ");
					else if(Prison.getEl(i, j) instanceof Exit)
						scrivi.print("U ");
				}
			}
			scrivi.print(Admin.player.getLife() + " "
			 + Admin.player.getForce() + " "
			 + Admin.player.getWeapon().tipo + " "
			 + Admin.player.getWeapon().danno
			 );
		}catch (IOException e) {
			System.out.println("Errore: " + e);
			System.exit(1);
		}
	}
	
	public void load() {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader("Salvataggio.txt"));
		} catch (FileNotFoundException e) {
			System.out.println("Errore: " + e);
			System.exit(1);
		}
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				String[] parts = line.split("\\s");
				int k=0;
					for (int i = 0; i < ROWS; i++) {
						for (int j = 0; j < COLUMN; j++) {
							tempArray[i][j] = parts[k];
							k++;
						}
					}
					Integer loadLife = Integer.valueOf(parts[(ROWS*COLUMN)]);
					Integer loadForce = Integer.valueOf(parts[(ROWS*COLUMN)+1]);
					String nomeArma = parts[(ROWS*COLUMN)+2];
					Integer forzaArma = Integer.valueOf(parts[(ROWS*COLUMN)+3]);
					Weapon tempArma = new Weapon (nomeArma,forzaArma);
				for (int i = 0; i < ROWS; i++) {
					for (int j = 0; j < COLUMN; j++) {
						if (tempArray[i][j].contains("G")) {
							Admin.player = new Player(loadLife,loadForce,tempArma);
							setEl(i,j,Admin.player);
						}
						else if (tempArray[i][j].contains("m")) {
							setEl(i,j, new Monster(50, 10, Admin.ascia));
						}
						else if (tempArray[i][j].contains("w")) {
							setEl(i,j, new Wall());
						}
						else if (tempArray[i][j].contains("p")) {
							Potion p = new Potion();
							setEl(i,j,p);
						}
						else if (tempArray[i][j].contains("U")) {
							setEl(i,j,exit);
						}
						else if (tempArray[i][j].contains("v")) {
							setEl(i,j, new Empty());
						}
						
					}
				}
			}
		} catch (IOException e) {
			System.out.println("Errore: " + e);
			System.exit(1);
		}

	}
}
